﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HengLiXiuJian : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
				if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					transaction = new Transaction(document, "横管立管三维修剪");
					transaction.Start();
					ISelectionFilter selectionFilter = new PipeFilter();
					Selection selection = activeUIDocument.Selection;
					for (;;)
					{
						try
						{
							Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择要修剪/延伸的第一根管道。（单击要保留的部分）");
							Reference reference2 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择要修剪/延伸的第二根管道。（单击要保留的部分）");
							XYZ globalPoint = reference.GlobalPoint;
							XYZ globalPoint2 = reference2.GlobalPoint;
							Pipe pipe = document.GetElement(reference) as Pipe;
							Pipe pipe2 = document.GetElement(reference2) as Pipe;
							if (!new YJKPiping<Pipe>(pipe2).IsParallel(pipe))
							{
								YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
								string text = "";
								if (YJKRevitTools.isRiserPipe(pipe2) && !YJKRevitTools.isRiserPipe(pipe))
								{
									hypipeConnectorHelper.TPipeConnectRPipe(pipe2, globalPoint2, pipe, globalPoint, document, ref text, false);
								}
								else if (YJKRevitTools.isRiserPipe(pipe) && !YJKRevitTools.isRiserPipe(pipe2))
								{
									hypipeConnectorHelper.TPipeConnectRPipe(pipe, globalPoint, pipe2, globalPoint2, document, ref text, false);
								}
								else if (!YJKRevitTools.isRiserPipe(pipe) && !YJKRevitTools.isRiserPipe(pipe2))
								{
									hypipeConnectorHelper.TPipeConnectRPipe(pipe, globalPoint, pipe2, globalPoint2, document, ref text, false);
								}
								document.Regenerate();
								if (text != "")
								{
									YJKMessageBox.Show(text);
								}
								continue;
							}
							YJKMessageBox.Show("不能修剪/延伸两根平行的管道！");
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(transaction, false);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				this.EndTransation(transaction, true);
			}
			return result;
		}

		private void EndTransation(Transaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
				if (status == (TransactionStatus)1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
				else if (status == (TransactionStatus)5)
				{
					tran.RollBack();
				}
			}
		}
	}
}
